<script setup lang="ts">
import { downloadAPI, EnterPriseDetailAPI } from '@/apis/park';
import type { EnterPriseDetailRes, Rent } from '@/types/park';
import { onMounted, ref } from 'vue';
import { useRoute } from 'vue-router';
const route=useRoute()
const Detailres=ref<EnterPriseDetailRes>({} as EnterPriseDetailRes)
onMounted(async() => {
  const res= await EnterPriseDetailAPI(route.params.id as string)
  Detailres.value=res.data
})
const getUrl=(url:string)=>{
  const urlType=url.split('.').pop()
  if(urlType==='pdf'){
    return url
  }else if(urlType==='doc'||urlType === 'docx'){
    const previewURL = "https://view.officeapps.live.com/op/view.aspx?src=";
   return previewURL+encodeURIComponent(url)
}
}
 const downLoad=async(row:Rent)=>{
  const blob=await downloadAPI(row.contractId)
  const aElement=document.createElement('a')
  aElement.href=URL.createObjectURL(blob)
  aElement.download=row.contractName
  aElement.click()
  URL.revokeObjectURL(aElement.href)
  aElement.remove()
 }

</script>
<template>
  <div class="detail-enterprise">
    <header class="add-header">
      <el-page-header content="查看企业" @back="$router.back()" />
    </header>
    <main class="add-main">
      <div class="form-container">
        <div class="title">租赁记录</div>
        <div class="table-container">
          <el-table :data="Detailres.rent" style="width: 100%" border>
            <el-table-column type="index" label="序号" width="50" />
            <el-table-column prop="name" label="租赁楼宇" width="180" />
            <el-table-column label="租赁起止时间" width="280">
              <template #default="{ row }">
                {{ row.startTime }} - {{ row.endTime }}
              </template>
            </el-table-column>
            <el-table-column label="租赁合同(点击预览)">
              <template #default="{ row }">
                <el-button type="primary" link>
                  <a :href="getUrl(row.contractUrl)" target="_blank" class="link">{{ row.contractName }}</a>
                </el-button>
              </template>
            </el-table-column>
            <el-table-column prop="createTime" label="录入时间" />
            <el-table-column prop="address" label="操作">
              <template #default="{ row }">
                <el-button type="primary" link @click="downLoad(row)">
                  合同下载
                </el-button>
              </template>
            </el-table-column>
          </el-table>
        </div>
      </div>
    </main>

  </div>
</template>


<style scoped lang="scss">
.detail-enterprise {
  background-color: #f4f6f8;
  height: 100vh;

  .add-header {
    display: flex;
    align-items: center;
    padding: 0 20px;
    height: 64px;
    background-color: #fff;

    .left {
      span {
        margin-right: 4px;
      }

      .arrow {
        cursor: pointer;
      }
    }

    .right {
      text-align: right;
    }
  }

  .add-main {
    background: #f4f6f8;
    padding: 20px 130px;

    .form-container {
      background-color: #fff;
      margin-bottom: 20px;

      .title {
        height: 60px;
        line-height: 60px;
        padding-left: 20px;
      }
    }

    .table-container {
      padding: 20px;
    }

    .preview {
      img {
        width: 100px;
      }
    }
  }
}
.link{
  text-decoration: none;
  color: inherit;
}
</style>
